<script lang="ts" setup>
  import { reactive, ref, watch } from "vue"
  import { getList,deleteByRoleId } from "@/api/role"
  import { type FormInstance, ElMessage, ElMessageBox } from "element-plus"
  import { usePagination } from "@/hooks/usePagination"
  import handle from './components/handle.vue'
  import useTable from '@/hooks/useTable'

  const loading = ref<boolean>(false)
  const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
  const dialogVisible = ref<boolean>(false)
  const activeId = ref<number>(0)
  const tableData = ref<any>([])

  const getTableData = () => {
    loading.value = true
    getList({
      ...paginationData,
    }).then((res:any) => {
      paginationData.total = res.totalCount
      tableData.value = res.data
    }).catch(() => {
      tableData.value = []
    }).finally(() => {
      loading.value = false
    })
  }

  const { handleSelectionChange,multipleSelection, handleBatchDelete, handleDelete } = useTable({getList:getTableData})

  //关闭操作框
  const closeHandle = () => {
    dialogVisible.value = false
  }

  //操作完成
  const successHandle = () => {
    dialogVisible.value = false
    getTableData()
  }

  //更新操作按钮
  const handleUpdate = (row: any) => {
    activeId.value = row.roleId
    dialogVisible.value = true
  }

  //新增操作按钮
  const handleAdd = () => {
    activeId.value = 0
    dialogVisible.value = true
  }

  watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <div class="title-wrapper">角色管理</div>
    <div class="page-main">
      <div class="toolbar-wrapper">
          <el-button type="primary" v-permission="['role-add']" @click="handleAdd">新增</el-button>
          <el-button type="info"  v-permission="['role-batch-del']" :disabled="!multipleSelection.length" @click="handleBatchDelete(deleteByRoleId,'roleId')">批量删除</el-button>
      </div>

      <div v-loading="loading"  class="table-wrapper">
        <el-table :data="tableData" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column sortable prop="name" label="角色名称"  />
          <el-table-column sortable prop="code" label="编码"  />
          <el-table-column sortable prop="isNormal" label="状态" >
            <template #default="scope">
              <el-tag v-if="scope.row.status === 0" type="success">启用</el-tag>
              <el-tag v-else type="danger">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150" >
            <template #default="scope">
              <el-button  v-if="scope.row.code != 'super'" type="primary" v-permission="['role-edit']" text size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button  v-if="scope.row.code != 'super'" type="danger" v-permission="['role-del']" text size="small" @click="handleDelete(deleteByRoleId, scope.row.roleId, scope.row.name)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>

  <handle
    v-if="dialogVisible"
    :id="activeId"
    @success="successHandle"
    @cancel="closeHandle"
    />
</template>

<style lang="scss" scoped>

</style>
